package com.gambi.tapestry.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public/* abstract */class GWTTapestry implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	private native TapestryGWTModule getTapestryGwtModuleCustomer(
			String moduleName)/*-{
								return $wnd.TapestryGWT.getModule( moduleName );
								}-*/;

	public void onModuleLoad() {

		GWT.log("GWTTapestry.onModuleLoad()");
		try {

			GWT.log("getName() = " + getName());
			TapestryGWTModule module = getTapestryGwtModuleCustomer(getName());

			int cCount = module.getComponentCount();
			GWT.log(" Must instantiate " + cCount);

			for (int i = 0; i < cCount; i++) {
				// Call the abstract method to instantiate the component.
				// We assume that the component will place itself somewhere in
				// the DOM
				widgetFactory(module.getComponentInitializerAt(i));
			}

		} catch (Throwable e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	// This is the default method. Overwrite in case you need a different module
	// id
	public String getName() {
		return this.getClass().getName();
	}

	// TODO This must be implemented by the sub classes. Can we Contribute to
	// Someone while creating a new component library
	// TODO Shall we define a EntryPointSource or something ?
	public/* abstract */void widgetFactory(
			TapestryGWTComponentInitializer component) {
	}/* ; */
}
